package com.block;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class TestBlock {
    private static volatile ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(5);

    public  void testOffer() {
        try {
            for (int i = 0; i < 10; i++) {
                boolean offer = queue.offer(i + "", 1,TimeUnit.SECONDS);
                System.out.println(Thread.currentThread().getName() + "==>" + offer+"--"+i);
//                Thread.sleep(200);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public  void testPoll() {
        try {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "---" + queue.poll(150,TimeUnit.MILLISECONDS)+"--"+i);
                Thread.sleep(200);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        final TestBlock test = new TestBlock();
        
        
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                test.testOffer();
            }
        });

        Thread t2 = new Thread(new Runnable() {
            public void run() {
                test.testPoll();
            }
        });
        t1.start();
        Thread.sleep(20000);
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
       
    }
}
